package com.litong.rms.action;


import java.util.List;

import com.litong.rms.bean.Role;
import com.litong.rms.bean.User;
import com.litong.rms.bean.UserRole;
import com.litong.rms.service.RoleService;
import com.litong.rms.service.UserRoleService;
import com.litong.rms.service.UserService;

public class UserAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -883118110323968234L;
	
	private User user;//用户实例
	
	private UserRole userRole;//用户角色实例
	
	private String userId;//用户ID
	
	private String roleId;//角色ID
	
	private String[] roleIdArray;//选中的角色ID
	
	private int[] selectedRoleId;//已经选中的角色ID
	
	private UserService userService;//用户服务
	
	private RoleService roleService;//角色服务
	
	private UserRoleService userRoleService;//用户角色服务
	
	private List<User> userList;//用户列表
	
	private List<Role> roleList;//角色列表
	
	private List<UserRole> userRoleList;//用户角色列表
	
	/*获得用户列表*/
	public String list(){
		userList = userService.getUserList();
		roleList = roleService.getRoleList();
		return SUCCESS;
	}
	
	/*添加用户*/
	public String addUser(){
		
		userService.addUser(user);//添加用户
		//为用户分配角色--将用户id和角色id插入roleUser表
		if(roleIdArray!=null){
			userRole = new UserRole();
			for(int i = 0;i<roleIdArray.length;i++){
				userRole.setUserId(user.getUserId());
				userRole.setRoleId(Integer.parseInt(roleIdArray[i]));
				userRoleService.addUserRole(userRole);
			}
		}
		return 	SUCCESS;
	};
	public String findUser(){
		
		userService.findUser(user);
		return SUCCESS;
	}
	
	
	/*删除用户*/
	public String deleteUser(){
		user = new User();
		user.setUserId(Integer.parseInt(userId));
		//首先删除用户角色表中的数据（根据用户id删除关于这个用户所有角色）保持数据一致性
		userRoleService.deleteUserRole("user",Integer.parseInt(userId));
		//再删出当前用户
		userService.deleteUser(user);
		
		return list();
	}
	
	
	/*查找根据ID返回用户*/
	public String findUpdateUser(){
		user = userService.findById(Integer.parseInt(userId));
		//根据用户ID查询用户已经分配的角色
		userRoleList = userRoleService.getUserRoleList(Integer.parseInt(userId));
		if(userRoleList!=null||userRoleList.isEmpty()){
			selectedRoleId = new int[userRoleList.size()];
			for(int i=0;i<userRoleList.size();i++){
				userRole = userRoleList.get(i);
				selectedRoleId[i] = userRole.getRoleId();
			}
		}
		//加载roleLis
		list();
		return SUCCESS;
	}
	
	
	/*修改用户*/
	public String updateUser(){
		//首先删除根据用户Id删除相关的用户所有角色在更新插入新的角色
		userRoleService.deleteUserRole("user",user.getUserId());
		if(roleIdArray!=null){
			userRole = new UserRole();
			for(int i = 0;i<roleIdArray.length;i++){
				userRole.setUserId(user.getUserId());
				userRole.setRoleId(Integer.parseInt(roleIdArray[i]));
				userRoleService.addUserRole(userRole);
			}
		}
		userService.updateUser(user);
		return SUCCESS;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserId() {
		return userId;
	}
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	public List<Role> getRoleList() {
		return roleList;
	}
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public RoleService getRoleService() {
		return roleService;
	}
	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}
	public String getRoleId() {
		return roleId;
	}
	public void setUserRoleService(UserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}
	public UserRoleService getUserRoleService() {
		return userRoleService;
	}
	public void setUserRole(UserRole userRole) {
		this.userRole = userRole;
	}
	public UserRole getUserRole() {
		return userRole;
	}
	public void setRoleIdArray(String[] roleIdArray) {
		this.roleIdArray = roleIdArray;
	}
	public String[] getRoleIdArray() {
		return roleIdArray;
	}
	public void setUserRoleList(List<UserRole> userRoleList) {
		this.userRoleList = userRoleList;
	}
	public List<UserRole> getUserRoleList() {
		return userRoleList;
	}
	public void setSelectedRoleId(int[] selectedRoleId) {
		this.selectedRoleId = selectedRoleId;
	}
	public int[] getSelectedRoleId() {
		return selectedRoleId;
	}
	

}
